/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Vista;

import Controlador.ControlProveedor;
import java.awt.Component;
import java.sql.SQLException;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Damian
 */
public class RegistrarProveedor extends javax.swing.JFrame {

    /**
     * Creates new form RegistrarProveedor
     */
    public RegistrarProveedor() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtEmpresa = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtResponsable = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCorreo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtCiudad = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCallePrincipal = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtCalleSecundaria = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtNumCasa = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registrar Proveedor");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Proveedor"));
        jPanel1.setLayout(new java.awt.GridLayout(8, 2, 10, 5));

        jLabel1.setText("Empresa:");
        jPanel1.add(jLabel1);
        jPanel1.add(txtEmpresa);

        jLabel2.setText("Teléfono:");
        jPanel1.add(jLabel2);
        jPanel1.add(txtTelefono);

        jLabel4.setText("Nombre del Responsable:");
        jPanel1.add(jLabel4);
        jPanel1.add(txtResponsable);

        jLabel3.setText("Correo:");
        jPanel1.add(jLabel3);
        jPanel1.add(txtCorreo);

        jLabel6.setText("Ciudad:");
        jPanel1.add(jLabel6);
        jPanel1.add(txtCiudad);

        jLabel5.setText("Calle Principal:");
        jPanel1.add(jLabel5);
        jPanel1.add(txtCallePrincipal);

        jLabel8.setText("Calle Secundaria:");
        jPanel1.add(jLabel8);
        jPanel1.add(txtCalleSecundaria);

        jLabel7.setText("Número de Casa:");
        jPanel1.add(jLabel7);
        jPanel1.add(txtNumCasa);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Iconos/dialog-ok-apply (2).png"))); // NOI18N
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        jPanel2.add(btnAceptar);

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Iconos/dialog-cancel (2).png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancelar);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if (camposLlenos()) {
            try {
                ControlProveedor obj=new ControlProveedor();
                obj.insertar(txtEmpresa.getText(), txtTelefono.getText(),
                        txtCorreo.getText(),txtCiudad.getText(), 
                        txtCallePrincipal.getText(),txtCalleSecundaria.getText(),
                        txtNumCasa.getText(), txtResponsable.getText());
                JOptionPane.showMessageDialog(rootPane, "Proveedor ingresado correctamente.");
                this.dispose();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Llene todos los campos.");
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private boolean camposLlenos(){
        boolean a=true;
        Component com[]=jPanel1.getComponents();
        //System.out.println(com.length);
        for (Component c : com) {
            //System.out.println(c.toString());
            if ((c) instanceof JTextField) {
                if (((JTextField) c).getText().isEmpty()) {
                    a=false;
                    break;
                }
            }
        }
        return a;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegistrarProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegistrarProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegistrarProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegistrarProveedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RegistrarProveedor().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtCallePrincipal;
    private javax.swing.JTextField txtCalleSecundaria;
    private javax.swing.JTextField txtCiudad;
    private javax.swing.JTextField txtCorreo;
    private javax.swing.JTextField txtEmpresa;
    private javax.swing.JTextField txtNumCasa;
    private javax.swing.JTextField txtResponsable;
    private javax.swing.JTextField txtTelefono;
    // End of variables declaration//GEN-END:variables
}
